﻿
using System.Configuration;
using System.Net.Mail;
using System.Text;
using LenderDocs.ViewModels;

namespace LenderDocs.Service
{
    public class LenderDocsAppService
    {
        public void EmailRequest(LenderDocRequest lenderDocRequest)
        {
            //Create message body
            var messageBody = new StringBuilder("A new document request has been submitted.  Here are the details:");
            messageBody.AppendLine();
            messageBody.AppendLine();

            //Get requestor type
            var userType = string.Empty;
            if (lenderDocRequest.IsHomeSeller)
            {
                userType = "Home Seller";
            }
            else if (lenderDocRequest.IsAppraiser)
            {
                userType = "Appraiser";
            }
            else if (lenderDocRequest.IsClosingAttorney)
            {
                userType = "Closing Attorney";
            }
            else if (lenderDocRequest.IsRealtor)
            {
                userType = "Realtor";
            }
            else if (lenderDocRequest.IsMortgageCoOrBroker)
            {
                userType = "Mortgage Company or Broker";
            }

            messageBody.AppendLine(string.Format("Requestor Type: {0}", userType));
            messageBody.AppendLine(string.Format("Requestor Name: {0}", lenderDocRequest.RequestorName));
            messageBody.AppendLine(string.Format("Phone: {0}", lenderDocRequest.Phone));
            messageBody.AppendLine(string.Format("Fax: {0}", lenderDocRequest.Fax));
            messageBody.AppendLine(string.Format("Email: {0}", lenderDocRequest.Email));
            messageBody.AppendLine(string.Format("Community: {0}", lenderDocRequest.CommunityName));
            messageBody.AppendLine(string.Format("Address Line 1: {0}", lenderDocRequest.Address1));
            messageBody.AppendLine(string.Format("Address Line 2: {0}", lenderDocRequest.Address2));
            messageBody.AppendLine(string.Format("City, State, Zip: {0}, {1} {2}", lenderDocRequest.City, lenderDocRequest.State, lenderDocRequest.Zip));
            messageBody.AppendLine(string.Format("Seller Name: {0}", lenderDocRequest.SellerName));
            messageBody.AppendLine(string.Format("Buyer(s) Names: {0}", lenderDocRequest.BuyerName));
            messageBody.AppendLine(string.Format("IP Address: {0}", lenderDocRequest.IpAddress));

            //Get package type
            //Get requestor type
            var packageType = string.Empty;
            if (lenderDocRequest.CustomFormsPackage)
            {
                packageType = "Custom Forms";
            }
            else if (lenderDocRequest.RealtorFormPackage)
            {
                packageType = "Realtor Form 2A12T";
            }
            else if (lenderDocRequest.UniformDisclosurePackage)
            {
                packageType = "Uniform Disclosure Package";
            }
            else if (lenderDocRequest.UnpaidAssessmentsPackage)
            {
                packageType = "Unpaid Assessment Package";
            }
            messageBody.AppendLine(string.Format("Package Type: {0}", packageType));

            messageBody.AppendLine(string.Format("Rush Delivery: {0}", lenderDocRequest.RushDelivery ? "Yes" : "No"));

            var message = new MailMessage
                                {
                                    From = new MailAddress(ConfigurationManager.AppSettings["EmailFrom"]),
                                    Subject = ConfigurationManager.AppSettings["EmailSubject"],
                                    Body = messageBody.ToString()
                                };
            message.To.Add(new MailAddress(ConfigurationManager.AppSettings["EmailTo"]));

            using (var client = new SmtpClient(ConfigurationManager.AppSettings["SMTPServer"]))
            {
                client.Send(message); 
            }
        }
    }
}
